package com.crm.service.impl;

import com.crm.dao.repositories.CustomerRepositorie;
import com.crm.domain.Customer;
import com.crm.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author misterWei
 * @create 2018年11月15号:10点10分
 * @mailbox mynameisweiyan@gmail.com
 */
@Service
@Transactional    //定区关联客户逻辑
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerRepositorie customerRepositorie;

    @Override
    public List<Customer> findIsNullfixedArea() {

        return customerRepositorie.findIsNullFixedArea();
    }

    @Override
    public List<Customer> findConfixedArea(String fixedAreaId) {
        return customerRepositorie.findByFixedAreaId(fixedAreaId);
    }

    @Override
    public void customerToFixedArea(String customerId, String fixedAreaId) {
        //由于客户的ID在前台属于 1,2,3的字符串进行传输的所以需要切割
        if ("null".equals(customerId) || customerId == null){
            customerRepositorie.customerIdAsNull(fixedAreaId);
            return;
        }
        String[] customerIds = customerId.split(",");
        for (String id : customerIds) {
            customerRepositorie.customerToFixedArea(Integer.parseInt(id.trim()), fixedAreaId);
        }
    }

    @Override
    public void saveCustomer(Customer customer) {
         customerRepositorie.save(customer);
    }

    @Override
    public Customer findTelephone(String telephone) {

        return customerRepositorie.findByTelephone(telephone);
    }

    @Override
    public void updateEmailStatus(String telephone) {
          customerRepositorie.updateEmailStatus(telephone);
    }

    @Override
    public Customer login(String telephone, String password) {
        return customerRepositorie.findByTelephoneAndPassword(telephone,password);
    }

    @Override
    public String findFixedAreaByAddress(String address) {

        return customerRepositorie.findFixedAreaByAddress(address);
    }
}
